var Theatre = function() {
	var current_hall_id   = null;
	var current_show_id   = null;
	var representation_id = null;

	var confirm_booking = null;
	var request_seats = null;


	function init() {
		confirm_booking = $('#confirm-booking');
		request_seats   = $('#request-seats');


		bindTriggers();

		defaults();
	}

	function bindTriggers() {
		$('#show_tabs ul.show_trigger_tab li.trigger a').on('shown', function (event) {
			event.preventDefault();

			var obj = $(this);

			var id = obj.attr('id').replace('hall_', '').replace('_trigger', '');
			var data = id.split('_');

			var hallId           = data[0];
			var showId           = data[1];
			var representationId = data[2];

			current_hall_id = hallId;
			current_show_id = showId;
			representation_id = representationId;

			var container = $('#hall_' + hallId + '_' + showId + '_' + representationId + '_content');

			container.html('<div class="please-wait"><i class="icon-refresh"></i> Please wait ...</div>');

			$.getJSON(
				base + '/booking/get',
				{
					hallId: hallId,
					showId: showId,
					representationId: representationId
				},
				function (data) {
					if (typeof(data['status']) == 'undefined' || data['status'] == 'failed') {
						alert('Error: ' . data['message']);

						return;
					}

					if (
						typeof(data.available_seats) == 'undefined' || !data.available_seats
						|| typeof(data.booked_seats) == 'undefined'
						|| typeof(data.unconfirmed_booked_seats) == 'undefined'
					) {
						return;
					}


					var content = '';
					for (var seat = 1; seat <= data.available_seats; seat++) {
						var type = 'badge-success';
						
						if (in_array(seat, data.booked_seats)) {
							type = 'badge-important';
						}
						else {
							if (in_array(seat, data.unconfirmed_booked_seats)) {
								type = 'badge-warning';
							}
						}

						content += '<span class="seat-no badge ' + type + '">' + seat + '</span>';
						if (seat % 8 == 0) {
							content += '<br style="clear: both" />';
						}
					}

					container.html(content);

					if (data.unconfirmed_booked_seats.length) {
						var unconfirmed_booked_seats = '';

						for (var i = 0; i < data.unconfirmed_booked_seats.length; i++) {
							unconfirmed_booked_seats += '<span class="seat-no inline badge badge-warning">' + data.unconfirmed_booked_seats[i] + '</span>';;
						}

						$('#selected_seats').show().find('span').html(unconfirmed_booked_seats);

						confirm_booking.show();
						request_seats.show();
					}
					else {
						$('#selected_seats').hide();

						confirm_booking.hide();
						request_seats.show();
					}
				}
			);
		});

		$('#hall_tabs ul.hall_trigger_tab li.trigger a').on('shown', function (event) {
			event.preventDefault();

			var obj = $(this);
			var hallId = obj.attr('id').replace('trigger_hall', '');

			$('#show_tabs #hall' + hallId + ' ul.show_trigger_tab li.trigger.active a').trigger('shown');
		});


		$('#user-login').validate({
			rules: {
				username: {
					required: true
				},
				password: {
					required: true
				}
			},
			messages: {
				username: {
					required: 'Fill in the username'
				},
				password: {
					required: 'Fill in the password'
				}
			}
		});

		confirm_booking.validate({
			rules: {
				name: {
					required: true
				}
			},
			messages: {
				name: {
					required: 'Fill in the name'
				}
			},
			submitHandler: function(form) {
				confirm_booking.find('.show_id').val(current_show_id);
				confirm_booking.find('.hall_id').val(current_hall_id);
				confirm_booking.find('.representation_id').val(representation_id);
				confirm_booking.find('.submit').css('visibility', 'hidden');

				$(form).ajaxSubmit({
					beforeSubmit: function() {},
					success: function(response) {
						confirm_booking.find('.submit').css('visibility', 'visible');
						$('#show_tabs ul.show_trigger_tab li.trigger.active a').trigger('shown');
						request_seats.find('#seats').val('');

						if (
							typeof(response) != 'undefined'
							|| typeof(response['status']) != 'undefined'
							|| response['status'] == 'error'
						) {
							if (typeof(response['message']) != 'undefined') {
								alert('Error: ' . response['message']);
							}

							return;
						}
					}
				});
			}
		});


		request_seats.validate({
			rules: {
				seats: {
					required: true,
					min: 1,
					number: 1
				}
			},
			messages: {
				seats: {
					required: 'Fill in the seats',
					min: 'Invalid seat number',
					number: 'Invalid seat number'
				}
			},
			submitHandler: function(form) {
				request_seats.find('.show_id').val(current_show_id);
				request_seats.find('.hall_id').val(current_hall_id);
				request_seats.find('.representation_id').val(representation_id);
				request_seats.find('.submit').css('visibility', 'hidden');

				$(form).ajaxSubmit({
					beforeSubmit: function() {},
					success: function(response) {
						request_seats.find('.submit').css('visibility', 'visible');
						$('#show_tabs ul.show_trigger_tab li.trigger.active a').trigger('shown');

						if (
							typeof(response) != 'undefined'
							|| typeof(response['status']) != 'undefined'
							|| response['status'] == 'error'
						) {
							return;
						}
					}
				});
			}
		});
	}

	function in_array(needle, haystack) {
		for(var i in haystack) {
			if(haystack[i] == needle) return true;
		}
		return false;
	}

	function defaults() {
		// load the active show
		$('#show_tabs ul.show_trigger_tab li.trigger.active a').trigger('shown');
		confirm_booking.hide();
	}

	return {
		init: init
	};
}();

$(document).ready(function() {
	Theatre.init();
});
